<?php
namespace Api\Controller;
use Think\Controller;

class IndexController extends Controller {
	public function _empty() {
		$data['status']  = 404;
		$data['message'] = '404 not found!';
		$this->ajaxReturn($data);
	}

    public function index() {
        $data['status']  = 200;
		$data['message'] = 'success';
		$this->ajaxReturn($data);
    }

    public function location() {
        $this->display();
    }

    public function viewlocation() {
        $installationId = I('iId', '');
        $this->assign('iId',$installationId);
        $this->display();
    }



    public function viewmap() {
        $installationId = I('iId', '');
        $this->assign('iId',$installationId);
        $this->display();
    }


    public function upload() {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     31457280 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            //$this->error($upload->getError());
            $data['status']  = 400;
            $data['message'] = $upload->getError();
            $this->ajaxReturn($data);
        }else{// 上传成功
            foreach($info as $file){
                //echo $file['savepath'].$file['savename'];

                $camera = M("Camera");
                $camera->poilist = I('installationId', '');
                $camera->savepath = I('savepath', $file['savepath'].$file['savename']);
                $camera->poilist = I('size', $file['size']);
                $camera->createTime = time();
                $camera->add();
            }
        }

        $data['status']  = 200;
        $data['message'] = 'success';
        $this->ajaxReturn($data);
    }


    public function cjdb() {
        $szUrl = "http://www.douban.com/group/haixiuzu/discussion?start=50";

        import('Common.Util.QueryList.QueryList');
        $reg = array("title"=>array(".title a:eq(0)","text"),"url"=>array(".title a:eq(0)","href"),"author"=>array("a:eq(1)","text"));
        $rang = ".olt tr";
        $hj = \QueryList::Query($szUrl, $reg, $rang,'curl','GBK');

        $arr = $hj->jsonArr;
        foreach ($arr as $k => $v) {
            if (empty($v['title'])) {
                unset($arr[$k]);
            } else {
                $ids = str_replace('http://www.douban.com/group/topic/', '', $v['url']);
                $arr[$k]['id'] = str_replace('/', '', $ids);
            }
        }

        echo "<pre>";
        print_r($arr);
        echo "</pre><hr/>";
    }

    public function cjwxsogou() {
        $url = "http://weixin.sogou.com/gzhjs?cb=sogou.weixin.gzhcb&openid=oIWsFt7bZjvilMTCNEEf_-YRHZao&ext=zMKRwhGG3zafJLO0EiAdZs0ZCEk1ObeIJEdzCMhwQqAXHJpxLI65NHgdAd0nZOA-&gzhArtKeyWord=&page=1&t=1444614518983";
        $UserAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIE, "CXID=140BC146BF3ECD761B80E60792746F38; SUID=5215FADC7F40900A555C1B6B0002DE60; SUV=1436927716382904; ABTEST=4|1444614462|v1; weixinIndexVisited=1; SNUID=0DE18BB3DCD8FD02583D89ECDD23549E; wapsogou_qq_nickname=; IPLOC=CN3501");
        curl_setopt($ch, CURLOPT_HEADER, 0);  //0表示不输出Header，1表示输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $html = curl_exec($ch);
        curl_close($ch);
var_dump($html);

//        preg_match_all('/sogou.weixin.gzhcb\((.*?)\]\}\)/',mb_convert_encoding($html, 'GBK', 'GBK,UTF-8,ASCII'),$Preferred_Name_USPS);

//        preg_match_all('/sogou.weixin.gzhcb\((.*?)\]\}\)/',$html,$Preferred_Name_USPS);
//
//        $json =  $Preferred_Name_USPS[1][0] . "]}";
//
//        $jsons = json_decode($json,true);
//
//        foreach ($jsons['items'] as $v) {
//            $xml = (mb_convert_encoding($v, 'GBK', 'GBK,UTF-8,ASCII'));
//
//            preg_match_all("/\<DOCUMENT\>(.*?)\<\/DOCUMENT\>/s", $xml, $bookblocks);
//
//            foreach ($bookblocks[1] as $block) {
//                preg_match_all("/\<title\>\<!\[CDATA\[(.*?)\]\]\>\<\/title\>/", $block, $title);
//                preg_match_all("/\<content168\>\<!\[CDATA\[(.*?)\]\]\>\<\/content168\>/", $block, $content168);
//                preg_match_all("/\<date\>\<!\[CDATA\[(.*?)\]\]\>\<\/date\>/", $block, $date);
//                preg_match_all("/\<url\>\<!\[CDATA\[(.*?)\]\]\>\<\/url\>/", $block, $url);
//
//                //echo ($this->get_head("http://weixin.sogou.com" . $url[1][0]));
//
//
//
//                echo('<br>');
//                echo($title[1][0] . '<br>');
//                echo($content168[1][0] . '<br>');
//                echo($date[1][0] . '<br>');
//                //echo($imglink[1][0] . '<br>');
//                echo($url[1][0] . '<br>');
//                echo('-----------------------<br><br>');
//            }
//        }
    }

    function get_head($sUrl)
    {
//        $oCurl = curl_init();
//        // 设置请求头, 有时候需要,有时候不用,看请求网址是否有对应的要求
//
//        $headers['Upgrade-Insecure-Requests'] = 1;
//        $headers['Host'] = 'weixin.sogou.com';
//        $headers['Cookie'] = 'CXID=140BC146BF3ECD761B80E60792746F38; SUID=5215FADC7F40900A555C1B6B0002DE60; SUV=1436927716382904; ABTEST=4|1444614462|v1; weixinIndexVisited=1; SNUID=0DE18BB3DCD8FD02583D89ECDD23549E; IPLOC=CN3502; wapsogou_qq_nickname=; LSTMV=245%2C539; LCLKINT=8996';
//
//        $user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
//
//        curl_setopt($oCurl, CURLOPT_URL, $sUrl);
//        curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
//        // 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
//        curl_setopt($oCurl, CURLOPT_HEADER, true);
//        // 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文
//        curl_setopt($oCurl, CURLOPT_NOBODY, true);
//        // 使用上面定义的 ua
//        curl_setopt($oCurl, CURLOPT_USERAGENT, $user_agent);
//        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
//        // 不用 POST 方式请求, 意思就是通过 GET 请求
//        curl_setopt($oCurl, CURLOPT_POST, false);
//
//        $sContent = curl_exec($oCurl);
//
//        // 获得响应结果里的：头大小
//        $headerSize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
//        // 根据头
//        $header = substr($sContent, 0, $headerSize);
//
//        curl_close($oCurl);
//
//        return $header;


//        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, $sUrl);
//// 不需要页面内容
//        curl_setopt($ch, CURLOPT_NOBODY, 1);
//// 不直接输出
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//// 返回最后的Location
//        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//        curl_exec($ch);
//        $info = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
//        curl_close($ch);
//        var_dump($info) ;

//
//        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, $sUrl);
//        curl_setopt($ch, CURLOPT_VERBOSE, true);
//        curl_setopt($ch, CURLOPT_HEADER, true);
//        curl_setopt($ch, CURLOPT_NOBODY, true);
//        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
//        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
//        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//        $ret = curl_exec($ch);
//        $info = curl_getinfo($ch);
//        curl_close($ch);
//
//        var_dump($info);

        $ch = curl_init();
        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL,  $sUrl);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch,  CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
//        curl_setopt($ch,  CURLOPT_POSTFIELDS, $vars);
        $data = curl_exec($ch);
        $Headers =  curl_getinfo($ch);
        curl_close($ch);

        if ($data != $Headers)
            return  $Headers["url"];
        else
            return false;
    }




}